package com.tsingteng.cosfun.ui.message.geneal;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.PlayBean;
import com.tsingteng.cosfun.bean.VideoBean;
import com.tsingteng.cosfun.common.Constant;
import com.tsingteng.cosfun.ui.cosfun.PlayActivity;
import com.tsingteng.cosfun.ui.message.comment.usercomment.UserCommentAdapter;
import com.tsingteng.cosfun.ui.message.geneal.presenter.PlayPresenter;
import com.tsingteng.cosfun.ui.mine.HomeMineFragment;
import com.tsingteng.cosfun.ui.mine.TerminalActivity;
import com.tsingteng.cosfun.utils.ImageUtils;
import com.tsingteng.cosfun.widget.MyRecycleView;
import com.tsingteng.cosfun.widget.dialog.BottomListDialog;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 描述 :
 * 创建人: 肖利.
 * 时间:2018/8/7
 * qq(872179966)
 */

public class PlayAdapter extends BaseQuickAdapter<PlayBean.PlayListBean, BaseViewHolder> {
    private PlayAdapter.OnAdapterFunctionListener onAdapterFunctionListener;
    private List<VideoBean> list = new ArrayList<>();

    public  PlayAdapter() {
        super(R.layout.item_play_adapter);
    }

    @Override
    protected void convert(BaseViewHolder helper, PlayBean.PlayListBean item) {
        CircleImageView headerView = helper.getView(R.id.iv_header);
        TextView name = helper.getView(R.id.tv_name);
        TextView operator = helper.getView(R.id.tv_operator);
        TextView tvContent = helper.getView(R.id.tv_content);
        TextView greatNum = helper.getView(R.id.tv_great_num);
        TextView videoNum = helper.getView(R.id.tv_video_count);
        TextView commentNum = helper.getView(R.id.tv_comment_num);
        TextView shareNum = helper.getView(R.id.tv_share_num);
        ImageView ivGreat = helper.getView(R.id.iv_great);
        ImageView iv_more = helper.getView(R.id.iv_more);
        ImageView guanzhu = helper.getView(R.id.iv_guan_zhu);
        ImageView iv_header = helper.getView(R.id.iv_header);
        ImageUtils.LoadImage(mContext, item.getImage(), headerView);
        setText(name, item.getNickName());
        setText(operator, item.getOperatorLabel());
        setText(tvContent, item.getContent());
        setText(greatNum, item.getPraiseCount());
        setText(commentNum, item.getCommentCount());
        setText(shareNum, item.getForwardCount());
        if (TextUtils.equals(item.getVideoCount(), "0")) {
            videoNum.setVisibility(View.GONE);
        } else {
            videoNum.setVisibility(View.VISIBLE);
            setText(videoNum, "翻拍视频 " + item.getVideoCount() + "个");
        }

        ImageUtils.LoadImage(mContext, item.getImage(), headerView);
        if (item.getPraiseStatus() == 0) {
            ivGreat.setImageDrawable(mContext.getResources().getDrawable(R.drawable.great_icon));
        } else {
            ivGreat.setImageDrawable(mContext.getResources().getDrawable(R.drawable.great_pink_icon));
        }
        MyRecycleView mRv = helper.getView(R.id.rv_video);
        mRv.isCanRefresh(false);
        mRv.setLayoutManager(new GridLayoutManager(mContext, 4));
        PlayVideoAdapter playVideoAdapter = new PlayVideoAdapter();
        mRv.setAdapter(playVideoAdapter);
        List<VideoBean> data = item.getVideoInfos().getData();
        if (list!= null) {
            list.clear();
        }else {
            list= new ArrayList<>();
        }
        if (data.size() > 4) {
            for (int i = 0; i < 4; i++) {
                list.add(data.get(i));
            }

        }else {
            list.addAll(data);
        }
        playVideoAdapter.addData(list);
        //更多
        helper.getView(R.id.rl_more).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                ((PlayFragment)context).getPresenter().
                if (onAdapterFunctionListener != null) {
                    onAdapterFunctionListener.onMoreClick(item);
                }
            }
        });
        //关注
        RelativeLayout rl_guan_zhu = helper.getView(R.id.rl_guan_zhu);
        TextView tv_play_guan_zhu = helper.getView(R.id.tv_play_guan_zhu);
        int relationType = item.getRelationType();

        if (relationType == 0) {
            tv_play_guan_zhu.setText("关注");
            guanzhu.setBackground(mContext.getResources().getDrawable(R.drawable.shape_bg_white));
            tv_play_guan_zhu.setTextColor(mContext.getResources().getColor(R.color.black));
        } else if (relationType == 1) {
            tv_play_guan_zhu.setText("已关注");
            guanzhu.setBackground(mContext.getResources().getDrawable(R.drawable.shape_trank_bg));
            tv_play_guan_zhu.setTextColor(mContext.getResources().getColor(R.color.white));
        } else if (relationType == 2) {
            tv_play_guan_zhu.setText("相互关注");
            guanzhu.setBackground(mContext.getResources().getDrawable(R.drawable.shape_pink_bg));
            tv_play_guan_zhu.setTextColor(mContext.getResources().getColor(R.color.color_found));
        } else if (relationType == -1) {
            rl_guan_zhu.setVisibility(View.INVISIBLE);

        }

        rl_guan_zhu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (relationType == 0) {
//                    tv_play_guan_zhu.setText("已关注");
//                    guanzhu.setBackground(mContext.getResources().getDrawable(R.drawable.shape_trank_bg));
//                    tv_play_guan_zhu.setTextColor(mContext.getResources().getColor(R.color.white));
//                    presenter.loginFollowFans(item.getProfileId() + "");
//                } else if (relationType == 1) {
//                    tv_play_guan_zhu.setText("关注");
//                    guanzhu.setBackground(mContext.getResources().getDrawable(R.drawable.shape_bg_white));
//                    tv_play_guan_zhu.setTextColor(mContext.getResources().getColor(R.color.black));
//                    presenter.loginNoFollow(item.getProfileId() + "");
//                } else if (relationType == 2) {
//                    tv_play_guan_zhu.setText("相互关注");
//                    guanzhu.setBackground(mContext.getResources().getDrawable(R.drawable.shape_pink_bg));
//                    tv_play_guan_zhu.setTextColor(mContext.getResources().getColor(R.color.color_found));
//                    presenter.loginNoFollow(item.getProfileId() + "");
//                } else if  (relationType == -1) {
//                    rl_guan_zhu.setVisibility(View.INVISIBLE);
//                }
                if (onAdapterFunctionListener != null) {
                    onAdapterFunctionListener.onAttentionClick(item,tv_play_guan_zhu,guanzhu);
                }
            }
        });
        //拍成剧
        helper.getView(R.id.iv_film_drama).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onAdapterFunctionListener != null) {
                    onAdapterFunctionListener.onFilmDrawClick(item);
                }

            }
        });
        //点赞
        helper.getView(R.id.ll_great).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onAdapterFunctionListener != null) {
                    onAdapterFunctionListener.onGreatClick(item, ivGreat, greatNum);
                }
            }
        });
        //评论
//        helper.getView(R.id.ll_comment).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (onAdapterFunctionListener != null) {
//                    onAdapterFunctionListener.onCommentClick(item);
//                }
//            }
//        });
        //分享
        helper.getView(R.id.ll_share).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onAdapterFunctionListener != null) {
                    onAdapterFunctionListener.onShareClick(item,shareNum);
                }
            }
        });

        //更多
        BottomListDialog dialog = new BottomListDialog(mContext);
        List<String> moreList = new ArrayList<>();
        moreList.add("举报");
        moreList.add("不喜欢");
        moreList.add("删除");
        dialog.setList(moreList);
        //头像\
        iv_header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int tag;
                if (item.getRelationType() == -1) {
                    tag = 0;
                } else {
                    tag = 1;
                }
                int profileId = item.getProfileId();
                Bundle bundle = new Bundle();
                bundle.putInt("tag", tag);
                bundle.putInt("user_id", profileId);
                bundle.putInt(Constant.TAG,1);
                TerminalActivity.showFragment(mContext, HomeMineFragment.class, bundle);
            }
        });
        playVideoAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(mContext, PlayActivity.class);
                intent.putExtra("type", 1);
                intent.putExtra("id", item.getId());
                intent.putExtra("position", position);
                mContext.startActivity(intent);
            }
        });
    }

    public void setText(TextView textView, String text) {
        if (textView != null && !text.isEmpty())
            textView.setText(text);
    }

    public void setOnAdapterFunctionListener(OnAdapterFunctionListener onAdapterFunctionListener) {
        this.onAdapterFunctionListener = onAdapterFunctionListener;
    }

    public interface OnAdapterFunctionListener {
        void onMoreClick(PlayBean.PlayListBean item);

        void onAttentionClick(PlayBean.PlayListBean item,TextView textView,ImageView imageView);

        void onFilmDrawClick(PlayBean.PlayListBean item);

        void onGreatClick(PlayBean.PlayListBean item, ImageView imageView, TextView greatNum);

        void onCommentClick(PlayBean.PlayListBean item);

        void onShareClick(PlayBean.PlayListBean item,TextView shareNum);
    }
}
